package com.xhtt.modules.inspection.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhtt.modules.inspection.dao.InsLinePointMapper;
import com.xhtt.modules.inspection.entity.InsLinePoint;
import com.xhtt.modules.inspection.entity.InsPoint;
import com.xhtt.modules.inspection.service.IInsLinePointService;
import com.xhtt.modules.inspection.service.IInsLineService;
import com.xhtt.modules.inspection.service.IInsPointLabelService;
import com.xhtt.modules.inspection.service.IInsPointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xj
 * @since 2019-01-18
 */
@Service
public class InsLinePointServiceImpl extends ServiceImpl<InsLinePointMapper, InsLinePoint> implements IInsLinePointService {

    @Autowired
    private IInsPointService insPointService;

    @Autowired
    private IInsPointLabelService insPointLabelService;

    @Autowired
    private IInsLineService insLineService;

    @Override
    public Integer getMaxSort(Integer lineId) {
        return baseMapper.getMax(lineId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void up(Integer pointId) {
        // 需要操作的检点
        InsLinePoint insLinePoint = baseMapper.selectById(pointId);
        Integer sort = insLinePoint.getSort();
        if (sort > 1) {
            insLinePoint.setSort(sort - 1);

            // 上面一个
            InsLinePoint insLinePoint1 = baseMapper.selectOne(new LambdaQueryWrapper<InsLinePoint>()
                    .eq(InsLinePoint::getSort, sort - 1));
            insLinePoint1.setSort(sort);

            baseMapper.updateById(insLinePoint);
            baseMapper.updateById(insLinePoint1);
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void down(Integer pointId) {

        // 需要操作的检点
        InsLinePoint insLinePoint = baseMapper.selectById(pointId);
        Integer sort = insLinePoint.getSort();
        insLinePoint.setSort(sort + 1);

        // 下面一个
        InsLinePoint insLinePoint1 = baseMapper.selectOne(new LambdaQueryWrapper<InsLinePoint>()
                .eq(InsLinePoint::getSort, sort + 1));
        if (insLinePoint1 != null) {

            insLinePoint1.setSort(sort);
            baseMapper.updateById(insLinePoint);
            baseMapper.updateById(insLinePoint1);
        }
    }

    @Override
    public List<InsPoint> getInsPointListWithoutInspect(Integer lineId) {
        List<InsPoint> insPointList = getInsPointListByLineId(lineId);
        return insPointList.stream().filter(InsPoint::getRequireFlag).collect(Collectors.toList());
    }

    @Override
    public List<InsPoint> getInsPointListByLineId(Integer lineId) {
        // 获取线路对应的所有的检测点id
        List<InsLinePoint> insLinePointList = baseMapper
                .selectList(new LambdaQueryWrapper<InsLinePoint>()
                        .eq(InsLinePoint::getLineId, lineId)
                        .orderByAsc(InsLinePoint::getSort));
        List<Integer> insPointIds = insLinePointList.stream()
                .map(InsLinePoint::getPointId).collect(Collectors.toList());
        // 查询
        List<InsPoint> insPointList = new ArrayList<>();

        for (Integer insPointId : insPointIds) {
            InsPoint insPoint = insPointService.getById(insPointId);
            if (insPoint != null) {
                insPointList.add(insPoint);
            }
        }

        // 关联表排序赋值给检测点表
        for (InsLinePoint insLinePoint : insLinePointList) {
            for (InsPoint insPoint : insPointList) {
                if (insPoint.getId().equals(insLinePoint.getPointId())) {
                    insPoint.setSort(insLinePoint.getSort());
                }
            }
        }
        // 根据sort排序
        insPointList.sort(Comparator.comparing(InsPoint::getSort));
        return insPointList;
    }

    @Override
    public List<InsPoint> getAllByLineId(Integer lineId) {
        // 获取线路对应的所有的检测点id
        List<InsLinePoint> insLinePointList = baseMapper
                .selectList(new LambdaQueryWrapper<InsLinePoint>()
                        .eq(InsLinePoint::getLineId, lineId)
                        .orderByAsc(InsLinePoint::getSort));
        List<Integer> insPointIds = insLinePointList.stream()
                .map(InsLinePoint::getPointId).collect(Collectors.toList());
        // 查询
        List<InsPoint> insPointList = new ArrayList<>();

        for (Integer insPointId : insPointIds) {
            InsPoint insPoint = insPointService.getAllById(insPointId);
            if (insPoint != null) {
                insPointList.add(insPoint);
            }
        }

        // 关联表排序赋值给检测点表
        for (InsLinePoint insLinePoint : insLinePointList) {
            for (InsPoint insPoint : insPointList) {
                if (insPoint.getId().equals(insLinePoint.getPointId())) {
                    insPoint.setSort(insLinePoint.getSort());
                }
            }
        }
        // 根据sort排序
        insPointList.sort(Comparator.comparing(InsPoint::getSort));
        return insPointList;
    }


}
